#ifndef YHAIDA_SOCKETSOPS_H
#define YHAIDA_SOCKETSOPS_H
#include "../logging/Logging.h"
#include "../base/Utils.h"
#include <arpa/inet.h>
#include <endian.h>
namespace yhaida
{
    namespace sockets
    {
        //大小端字节的转换
        inline uint64_t hostToNetwork64(uint64_t host64)
        {
            //  (无符号)64字节主机（小端）转网络（大端）：
            return htobe64(host64);
        }
        inline uint32_t hostToNetwork32(uint32_t host32)
        {
            return htonl(host32);
        }
        inline uint16_t hostToNetwork16(uint16_t host16)
        {
            return htons(host16);
        }
        //网络->主机
        inline uint64_t networkToHost64(uint64_t net64)
        {
            return be64toh(net64);
        }
        inline uint32_t networkToHost32(uint32_t net32)
        {
            return ntohl(net32);
        }
        inline uint16_t networkToHost16(uint16_t net16)
        {
            return ntohs(net16);
        }
        ///////////////////////////other functions////////////////////////
        // 1.创建
        int createNonblockingOrDie();
        // 2.连接
        void bindOrDie(int sockfd, const struct sockaddr_in &addr);
        // 3.监听
        void listenOrDie(int sockfd);
        // 4.accept
        int accept(int sockfd, struct sockaddr_in *addr);
        // 5.关闭
        void close(int sockfd);
        void shutdownWrite(int sockfd);
        //目的IP地址与端口
        void toHostPort(char *buff, size_t size, const struct sockaddr_in &addr);
        //源IP地址与端口
        void fromHostPort(const char *ip, uint16_t port, struct sockaddr_in *addr);

        struct sockaddr_in getLocalAddr(int sockfd);
        int getSocketError(int sockfd);

        //客户端
        int  connect(int sockfd, const struct sockaddr_in& addr);
        struct sockaddr_in getPeerAddr(int sockfd);
        bool isSelfConnect(int sockfd);
    }

}

#endif